#include <antlr3.h>
#include "PythonParser.h"
#include "PythonLexer.h"
#include "PythonTokenSource.h"

int main(int argc, char** argv) {
	pANTLR3_INPUT_STREAM input;
	pANTLR3_COMMON_TOKEN_STREAM tstream;
	pPythonParser psr;
	pPythonLexer lxr;
	SPLexContext lexCtx;

	input = antlr3AsciiFileStreamNew(argv[1]);
	if (input == NULL)
	{
		fprintf(stderr, "File not found: %s\n", (char *)argv[1]);
		exit(1);
	}
	lxr = PythonLexerNew(input);
	if (lxr == NULL)
	{
		fprintf(stderr, "Unable to create the lexer due to malloc() failure1\n");
		exit(ANTLR3_ERR_NOMEM);
	}
	initSPLexContext(&lexCtx);
	lexCtx.oldNextToken = TOKENSOURCE(lxr)->nextToken;
	TOKENSOURCE(lxr)->nextToken = &spNextToken;
	lxr->pLexer->rec->state->userp = &lexCtx;
	tstream = antlr3CommonTokenStreamSourceNew(ANTLR3_SIZE_HINT, TOKENSOURCE(lxr));
	tstream->discardOffChannel = ANTLR3_TRUE;

	if (tstream == NULL)
	{
		fprintf(stderr, "Out of memory trying to allocate token stream\n");
		exit(ANTLR3_ERR_NOMEM);
	}

	psr = PythonParserNew(tstream);
	if (psr == NULL) {
		fprintf(stderr, "Out of memory trying to allocate parser\n");
		exit(ANTLR3_ERR_NOMEM);
	}
	psr->file_input(psr);
	fflush(stdout);
	psr->free(psr); psr = NULL;
	tstream->free(tstream); tstream = NULL;
	lxr->free(lxr); lxr = NULL;
	input->free(input); input = NULL;
	freeSPLexContext(&lexCtx);
	Sleep(100000);
}